package ru.ibrae.mchs.radiation.common.excelEntities.zonage;

import ru.ibrae.mchs.radiation.common.PokazatelMap;
import ru.ibrae.mchs.radiation.entities.controllers.GeoobjectJpaController;
import ru.ibrae.mchs.radiation.entities.controllers.ResultJpaController;
import java.util.ArrayList;
import java.util.HashMap;

public class ExcelSubjectZonage {

    private String subjectRf;
    private HashMap<Integer, ArrayList<Integer>> zones = new HashMap<Integer, ArrayList<Integer>>();
    private ResultJpaController rjc = new ResultJpaController();
    private GeoobjectJpaController gjc = new GeoobjectJpaController();

    public ExcelSubjectZonage(String subjectRf) {
        this.subjectRf = subjectRf;
        for (int i = 1; i < 5; i++) {
            ArrayList<Integer> put = zones.put(i, new ArrayList<Integer>());
        }
    }

    public String getSubjectRf() {
        return subjectRf;
    }

    public void setSubjectRf(String subjectRf) {
        this.subjectRf = subjectRf;
    }

    public HashMap<Integer, ArrayList<Integer>> getZones() {
        return zones;
    }

    public void setZones(HashMap<Integer, ArrayList<Integer>> zones) {
        this.zones = zones;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof String) {
            String string = (String) obj;
            if (string.equalsIgnoreCase(subjectRf)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.getSubjectRf() != null ? this.getSubjectRf().hashCode() : 0);
        return hash;
    }

    public ArrayList<Integer> getCitiesListByZoneId(Integer zoneId) {
        if (zones.get(zoneId) == null) {
            ArrayList<Integer> put = zones.put(zoneId, new ArrayList<Integer>());
        }
        return zones.get(zoneId);
    }

    public Double getTotalPopul(PokazatelMap pm) {
        Double population = Double.valueOf(0);
        for (int i = 1; i < 5; i++) {
            for (Integer geoId : zones.get(i)) {
                String currentRes = rjc.getResByIdPIdGeoobjeIdTime(pm.getPokazatel().getIdP(), geoId, pm.getTime().getIdTime());
                population += currentRes.equals("") ? 0 : Integer.parseInt(currentRes);
            }
        }
        return population == 0 ? null : population;
    }

    public Integer getTotalCitiesNumber() {
        Integer num = 0;
        for (int i = 1; i < 5; i++) {
            num += getCitiesListByZoneId(i).isEmpty() ? 0 : zones.get(i).size();
        }
        return num == 0 ? null : num;
    }

    public Double getCommonPopulByZone(Integer zoneId, PokazatelMap pm) {
        Double population = 0.0;
        for (Integer geoId : zones.get(zoneId)) {
            String currentRes = rjc.getResByIdPIdGeoobjeIdTime(pm.getPokazatel().getIdP(), geoId, pm.getTime().getIdTime());
            population += currentRes.equals("") ? 0 : Integer.parseInt(currentRes);
        }
        return population == 0 ? null : population;
    }

    public Integer getCommonCititesNumberByZone(Integer zoneId) {
        return getCitiesListByZoneId(zoneId).isEmpty() ? null : zones.get(zoneId).size();
    }

    public Double getTotalActivePopul(PokazatelMap pm) {
        Double population = 0.0;
        for (int i = 1; i < 5; i++) {
            for (Integer geoId : zones.get(i)) {
                if (gjc.findGeoobject(geoId).getComment() == null) {
                    String currentRes = rjc.getResByIdPIdGeoobjeIdTime(pm.getPokazatel().getIdP(), geoId, pm.getTime().getIdTime());
                    population += currentRes.equals("") ? 0 : Integer.parseInt(currentRes);
                }
            }
        }
        return population == 0 ? null : population;
    }

    public Integer getTotalActiveCitiesNumber() {
        Integer num = 0;
        for (int i = 1; i < 5; i++) {
            if (!getCitiesListByZoneId(i).isEmpty()) {
                for (Integer geoId : getCitiesListByZoneId(i)) {
                    if (gjc.findGeoobject(geoId).getComment() == null) {
                        ++num;
                    }
                }
            }
        }
        return num == 0 ? null : num;
    }

    public Integer getActiveCititesNumberByZone(Integer zoneId) {
        if (getCitiesListByZoneId(zoneId).isEmpty()) {
            return null;
        } else {
            int num = 0;
            for (Integer geoId : getCitiesListByZoneId(zoneId)) {
                if (gjc.findGeoobject(geoId).getComment() == null) {
                    ++num;
                }
            }
            return num == 0 ? null : num;
        }
    }

    public Double getActivePopulByZone(Integer zoneId, PokazatelMap pm) {
        Double population = 0.0;
        for (Integer geoId : zones.get(zoneId)) {
            if (gjc.findGeoobject(geoId).getComment() == null) {
                String currentRes = rjc.getResByIdPIdGeoobjeIdTime(pm.getPokazatel().getIdP(), geoId, pm.getTime().getIdTime());
                population += currentRes.equals("") ? 0 : Integer.parseInt(currentRes);
            }
        }
        return population == 0 ? null : population;
    }

    public Integer getTotalRemovedCitiesNumber() {
        Integer num = 0;
        for (int i = 1; i < 5; i++) {
            if (!getCitiesListByZoneId(i).isEmpty()) {
                for (Integer geoId : getCitiesListByZoneId(i)) {
                    if (gjc.findGeoobject(geoId).getComment() != null && gjc.findGeoobject(geoId).getComment().getComentary1().startsWith("Упразднен")) {
                        ++num;
                    }
                }
            }
        }
        return num == 0 ? null : num;
    }

    public Integer getRemovedCititesNumberByZone(Integer zoneId) {
        if (getCitiesListByZoneId(zoneId).isEmpty()) {
            return null;
        } else {
            int num = 0;
            for (Integer geoId : getCitiesListByZoneId(zoneId)) {
                if (gjc.findGeoobject(geoId).getComment() != null && gjc.findGeoobject(geoId).getComment().getComentary1().startsWith("Упразднен")) {
                    ++num;
                }
            }
            return num == 0 ? null : num;
        }
    }

    public Double getRemovedPopulByZone(Integer zoneId, PokazatelMap pm) {
        Double population = 0.0;
        for (Integer geoId : zones.get(zoneId)) {
            if (gjc.findGeoobject(geoId).getComment() != null && gjc.findGeoobject(geoId).getComment().getComentary1().startsWith("Упразднен")) {
                String currentRes = rjc.getResByIdPIdGeoobjeIdTime(pm.getPokazatel().getIdP(), geoId, pm.getTime().getIdTime());
                population += currentRes.equals("") ? 0 : Integer.parseInt(currentRes);
            }
        }
        return population == 0 ? null : population;
    }

    public Double getTotalRemovedPopul(PokazatelMap pm) {
        Double population = 0.0;
        for (int i = 1; i < 5; i++) {
            for (Integer geoId : zones.get(i)) {
                if (gjc.findGeoobject(geoId).getComment() != null && gjc.findGeoobject(geoId).getComment().getComentary1().startsWith("Упразднен")) {
                    String currentRes = rjc.getResByIdPIdGeoobjeIdTime(pm.getPokazatel().getIdP(), geoId, pm.getTime().getIdTime());
                    population += currentRes.equals("") ? 0 : Integer.parseInt(currentRes);
                }
            }
        }
        return population == 0 ? null : population;
    }
}
